/*数字增加动画;数字千分位格式化
 *carbonReduce 碳排放量 fireTime 避免火灾次数
 */

function NumAutoPlusAnimation(targetEle, options) {
    //不传配置就把它绑定在相应html元素的data-xxxx属性上吧  
    options = options || {};

    var $this = document.getElementById(targetEle),
        time = options.time || $this.data('time'), //总时间--毫秒为单位  
        finalNum = options.num || $this.data('value'), //要显示的真实数值  
        regulator = options.regulator || 100, //调速器，改变regulator的数值可以调节数字改变的速度  

        step = finalNum / (time / regulator),
        /*每30ms增加的数值--*/
        count = 0, //计数器  
        initial = 0;

    var num, str_num, result;

    var timer = setInterval(function() {

        count = count + step;

        if (count >= finalNum) {
            clearInterval(timer);
            count = finalNum;

            /*数字千分位格式化加，例如：33，231*/
            num = finalNum;
            str_num = num.toString();
            result = "";
            while (str_num.length > 3) {
                result = "," + str_num.slice(-3) + result;
                str_num = str_num.slice(0, str_num.length - 3);
            }

        }

        //t未发生改变的话就直接返回  
        //避免调用text函数，提高DOM性能  
        var t = Math.floor(count);
        if (t == initial) return;

        initial = t;

        //最终显示加了","的数值
        if (str_num) {
            initial = str_num + result;
        }
        $this.innerHTML = initial;
    }, 30);
}

NumAutoPlusAnimation("carbonReduce", {
    time: 1500,
    num: 33231,
    regulator: 50
});

NumAutoPlusAnimation("fireTime", {
    time: 1500,
    num: 3231,
    regulator: 50
});


$(function(){
    $.ajax({
        type: 'post',
        url: 'https://wapp.mofisher.com/v2/office/recommend',
        data: {

        },
        dataType: 'jsonp',
        jsonp: 'callback',
        success: function(result) {
            //console.log(result);
            var datas = result.datainfo.news;
            for (var i = 0; i < datas.length; i++) {
                var list = '<div class="news-item"><a href="news-detail.html?id=' + datas[i].id + '"><div class="item-box"><div class="box-img"><div class="imgScale" style="background: url('+ datas[i].img_url +') 100% 100%;background-repeat: no-repeat;background-position: center;background-size: cover;"></div></div>'+
                        '<div class="box-head bigText ofh">'+ datas[i].title +'</div></div></a></div>';
                $('.J_indexNews').append(list);
            }
        },
        error: function(e) {

        }
    });

});
